package com.bigdata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HBaseDDLDemo {

    private Configuration conf;
    // HBase 的连接对象
    private Connection conn;
    // HBase 操作DDL的对象
    private Admin admin;

    @Before
    public void init() throws Exception {
        conf = HBaseConfiguration.create();
        // 设置zk的连接信息
        conf.set("hbase.zookeeper.quorum", "node02,node03,node04");
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
    }

    /**
     * 删除表
     */
    @Test
    public void deleteTableTest() throws Exception {
        TableName tableName = TableName.valueOf("api_test:stu11111");
        // 如果表存在才能删除
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        } else {
            System.out.println("表不存在，无法删除");
        }

    }

    /**
     * 预分区创建表
     */
    @Test
    public void createsplitTableTest() throws Exception {
        // 创建TableNam对象
        TableName tableName = TableName.valueOf("api_test:t_split");
        // 表的描述信息
        // create 't1', 'cf'
        TableDescriptorBuilder tableBuilder = TableDescriptorBuilder.newBuilder(tableName);

        List<ColumnFamilyDescriptor> list = new ArrayList<>();

        // 追加列族信息
        ColumnFamilyDescriptorBuilder cfBuilder = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes());
        list.add(cfBuilder.build());

        // 把列族的集合设置到tableDesc中
        tableBuilder.setColumnFamilies(list);

        // 切分Region的标识
        byte[][] splitKeys = {{'a', 'b'}, {'d', 'e'}};

        // 追加预分区信息
        admin.createTable(tableBuilder.build(), splitKeys);
    }

    /**
     * 创建表
     */
    @Test
    public void createTableTest() throws Exception {
        String[] arr = {"info"};
        createTable("api_test:student", arr);
    }


    private void createTable(String tableNameStr, String[] cfs) throws Exception {
        // 创建TableNam对象
        TableName tableName = TableName.valueOf(tableNameStr);
        // 表的描述信息
        // create 't1', 'cf'
        TableDescriptorBuilder tableBuilder = TableDescriptorBuilder.newBuilder(tableName);

        List<ColumnFamilyDescriptor> list = new ArrayList<>();

        for (String cf : cfs) {
            // 追加列族信息
            ColumnFamilyDescriptorBuilder cfBuilder = ColumnFamilyDescriptorBuilder.newBuilder(cf.getBytes());
            list.add(cfBuilder.build());
        }

        // 把列族的集合设置到tableDesc中
        tableBuilder.setColumnFamilies(list);

        admin.createTable(tableBuilder.build());
    }


    /**
     * 判断表格是否存在
     */
    @Test
    public void existsTable() throws Exception {
        // 创建TableNam对象
        TableName tableName = TableName.valueOf("default:t_split");

        boolean flag = admin.tableExists(tableName);
        System.out.println(flag);
    }

    /**
     * 创建命名空间
     */
    @Test
    public void createNsTest() throws Exception {
        System.out.println(conn);
        System.out.println(admin);

        // 建造者模式
        NamespaceDescriptor.Builder nsBuilder = NamespaceDescriptor.create("api_test");

        admin.createNamespace(nsBuilder.build());
    }


    @After
    public void close() throws IOException {
        admin.close();
        conn.close();
    }
}
